﻿//_______________________________________________________________________________________________________________________
// Copy Right (c) 2010, 2011 SapiensIT Consulting GmbH, all rights reserved
//_______________________________________________________________________________________________________________________
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//_______________________________________________________________________________________________________________________
// PROJECT:     sapiens.at.FrameworkSample (SharePoint 2010/V4)
//_______________________________________________________________________________________________________________________
// VERSION:     2.0
//_______________________________________________________________________________________________________________________
// DESCRIPTION: 
//_______________________________________________________________________________________________________________________
// LICENSE:     Different licens Types apply to different file types:
//               - Source code files are governed by the MICROSOFT PUBLIC LICENSE (Ms-PL)
//               - Binary files are governed by MSDN CODE GALLERY BINARY LICENSE
//               - Documentation files are governed by CREATIVE COMMONS ATTRIBUTION 3.0 LICENSE
//
//               The licenses can be found at the projects source location.
//               If you do not agree, don't use it!
// 
//_______________________________________________________________________________________________________________________
// Start SharePoint Branding, visit http://www.cliggin.com! Visit us on http://www.sapiens.at! Tell your friends! 
//_______________________________________________________________________________________________________________________
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sapiens.at.SharePoint.CustomActions;
using Sapiens.at.FrameworkSample.Entities;
using System.Web.UI;
using Microsoft.SharePoint;
using Sapiens.at.SharePoint.Repositories;
using System.Text.RegularExpressions;
using Microsoft.SharePoint.Utilities;
using System.Web;

namespace Sapiens.at.FrameworkSample.CustomActions.AddEmployeeForm
{
    class AddEmployee : RibbonAction
    {
        public AddEmployee()
        {
            RegisterCommand(new CustomActionConfig("Sapiens.PageComponent.Command.AddEmployeeForm")
            {
                IsApplicable = IsApplicable,
                //CallbackScript = "window.location.href = '../../Lists/Employees/NewForm.aspx?Source=' + encodeURIComponent(window.location.href + '?SelectedID=' + selectedId)",
                Callback = OnAddEmployeeFormClicked
            });
        }

        private bool IsApplicable()
        {
            return IsApplicableListItem(new string[] { Department.ListUrl });
        }

        private void OnAddEmployeeFormClicked(string command, Page page)
        {
            var source = HttpUtility.UrlEncode(page.Request.QueryString["Source"] + "?SelectedID=" + SPContext.Current.ItemId);
            var url = string.Format("../../{0}?Source={1}&IsDlg=1", 
                new ListRepository(Employee.ListUrl, SPContext.Current.Web).List.Forms[PAGETYPE.PAGE_NEWFORM].Url,
                source);
            page.Response.Redirect(url, true);
        }
    }
}
